package com.zhaolulu.service.impl;

import cn.elegent.pay.CallBackService;
import com.zhaolulu.common.constant.OrderStatus;
import com.zhaolulu.common.constant.PayStatus;
import com.zhaolulu.domain.Order;
import com.zhaolulu.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 火飞鹰
 * @CSDN https://blog.csdn.net/weixin_44811903
 * @date 2025/7/6 0:11
 * @Description
 */
@Service
@Slf4j
public class CallBackServiceImpl implements CallBackService {

    @Autowired
    private OrderService orderService;


    @Override
    public void successPay(String orderSn) {
        log.info("支付成功回调{}", orderSn);

        Order order = orderService.getByOrderNo(orderSn);
        if (order != null) {
            if (order.getStatus().equals(OrderStatus.ORDER_STATUS_CREATE)) {
                order.setStatus(OrderStatus.ORDER_STATUS_PAYED); //订单状态  已支付
                order.setPayStatus(PayStatus.PAY_STATUS_PAYED);//支付状态  成功
                orderService.updateById(order);
                //todo 发货
            }
        }
    }

    @Override
    public void failPay(String orderSn) {
        log.info("支付失败回调", orderSn);
    }
    /**
     * 修改订单状态为退款完成
     * @param orderSn
     */
    @Override
    public void successRefund(String orderSn) {
        log.info("退款成功回调！" + orderSn);
        Order order = orderService.getByOrderNo(orderSn);
        if (order != null) {
            order.setPayStatus(PayStatus.PAY_STATUS_REFUNDIED);// 支付状态：退款成功
            orderService.updateById(order);
        }
    }

    /**
     * 退款失败打日志记录--修改状态为已支付
     * @param orderSn
     */
    @Override
    public void failRefund(String orderSn) {
        log.info("退款失败回调！" + orderSn);
        Order order = orderService.getByOrderNo(orderSn);
        if (order != null) {
            order.setPayStatus(PayStatus.PAY_STATUS_REFUNDING);// 支付状态：退款成功
            orderService.updateById(order);
        }
    }
}